package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjGoodsTypeMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjGoodsTypeService;
import com.zb.service.ZjProductService;
import com.zb.service.ZjSpareService;
import com.zb.vo.GoodsTypeListVo;
import com.zb.vo.GoodsTypeSelectVo;
import com.zb.vo.GoodsTypeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-08-01
 */
@Service
public class ZjGoodsTypeServiceImpl extends ServiceImpl<ZjGoodsTypeMapper, ZjGoodsType> implements ZjGoodsTypeService {
    @Resource
    private ZjGoodsTypeMapper goodsTypeMapper;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjSpareService spareService;

    // 新增或修改商品类型
    @Override
    public void save(Long custAccId, GoodsTypeVo typeVo) {
        Assert.isTrue(null == goodsTypeMapper.existByName(custAccId, typeVo.getId(), typeVo.getName()), "类型名称已经重复", typeVo.getName());
        ZjGoodsType type = new ZjGoodsType();
        BeanUtils.copyProperties(typeVo, type);
        type.setDisabled(typeVo.getStatus());
        if (null != type.getId()) {
            this.updateById(type);
            productService.updateTypeByTypeId(type.getId(), type.getName());
            spareService.updateTypeByTypeId(type.getId(), type.getName());
            return;
        }
        type.setId(IdWorker.getId());
        type.setCustAccId(custAccId);
        this.save(type);
    }

    // 获取商品类型列表
    @Override
    public Map<String, Object> getList(Long custAccId, GoodsTypeSelectVo selectVo) {
        QueryWrapper<ZjGoodsType> wrapper = new QueryWrapper<>();
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setPageLimit(selectVo);
        Page<ZjGoodsType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);

        List<GoodsTypeListVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(GoodsTypeListVo.beanToVo(t)));

        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }

    // 批量禁用或启用商品类型
    @Override
    public void updateBatchDisableByIds(List<Long> idList, boolean disable) {
        goodsTypeMapper.updateBatchDisableByIds(idList, disable);
    }

    // 批量删除商品类型
    @Override
    public void deleteByIds(List<Long> idList) {
        List<ZjProduct> productList = productService.getRelationTypeByTypeIds(idList);
        Assert.isTrue(CollectionUtil.isEmpty(productList),
                "删除的商品类型[ "+ productList.stream().map(ZjProduct::getType).collect(Collectors.joining("、"))
                        +" ]已与产品型号[ "+ productList.stream().map(ZjProduct::getModel).collect(Collectors.joining("、")) +" ]关联使用中，不允许删除");
        List<ZjSpare> spareList = spareService.getRelationTypeByTypeIds(idList);
        Assert.isTrue(CollectionUtil.isEmpty(spareList),
                "删除的商品类型[ "+ spareList.stream().map(ZjSpare::getType).collect(Collectors.joining("、"))
                        +" ]已与备件型号[ "+ spareList.stream().map(ZjSpare::getModel).collect(Collectors.joining("、")) +" ]关联使用中，不允许删除");
       this.removeByIds(idList);
    }

    // 分页获取商品分类数据下拉列表
    @Override
    public Map<String, Object> pageSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjGoodsType> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name");
        wrapper.eq("cust_acc_id", custAccId);
        wrapper.eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjGoodsType> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        List<SelectResultVo> voList = new ArrayList<>();
        page.getRecords().forEach(t -> voList.add(new SelectResultVo(t.getId(), t.getName())));
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), voList);
    }
}
